<?php
/**
 * 续费管理
 * 
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class XufeiController extends BaseController {
	public function index(){
		//列表
		$arr = $_GET;
		$Customer = D("CustomerXufei");
		$list = $Customer->index($arr,$this->m_is_admin,$this->m_psize);
		e_json($list);
	}
	public function Change_Star(){
		//修改星标
		$CustomerXufei = M("customer_xufei");
		$where['customer_xufei_id'] = I("customer_xufei_id");
		$old_flag = $CustomerXufei->field("customer_flag")->where($where)->find();
		if($old_flag['customer_flag']=="1"){
			$map['customer_flag'] = '0';
			
		}else{
			$map['customer_flag'] = '1';
		}
		
		$list = $CustomerXufei->where($where)->save($map);
		if($list){
			$res['rcode'] = "1";
		}else{
			$res['rcode'] = "2";
		}
		$res['old_flag'] = $old_flag['customer_flag'];
		$res['xufei_id'] = I("customer_xufei_id"); 
		e_json($res);
	}
	public function CustomerTypeShow(){
		$number = I('get.psize', $this->$m_psize,'intval');
		if( !$number )
			$number = $this->m_psize;
		else 
			$number = I("get.psize");

		//修改客户类型加载
		$CustomerXufei = M("customer_xufei");
		$where['customer_xufei.customer_xufei_id'] = I("customer_xufei_id");
		$res['list'] = $CustomerXufei
					->field("customer_xufei.customer_xufei_id,customer_xufei.customer_id,customer_xufei.customer_flag,customer_xufei.customer_type,customer.note,customer.mount")
					->join("customer ON customer_xufei.customer_id = customer.customer_id")
					->where($where)
					->find();
		//全部客户类型
		$customer = array();
		$customer_type['0']['name'] = "新资源";
		$customer_type['0']['id'] = "1";
		$customer_type['1']['name'] = "A类客户";
		$customer_type['1']['id'] = "2";
		$customer_type['2']['name'] = "B类客户";
		$customer_type['2']['id'] = "3";
		$customer_type['3']['name'] = "C类客户";
		$customer_type['3']['id'] = "4";
		$customer_type['4']['name'] = "D类客户";
		$customer_type['4']['id'] = "5";
		$customer_type['5']['name'] = "定金客户";
		$customer_type['5']['id'] = "6";
		$customer_type['6']['name'] = "完款客户";
		$customer_type['6']['id'] = "7";
		$customer_type['7']['name'] = "Z类客户";
		$customer_type['7']['id'] = "8";
		$res['customer_type'] = $customer_type;

		//沟通记录表
		$res['getdata'] = $_GET;
		$CustomerContactLog = M("customer_contact_log");
	
		if(1 == $this->m_user['user_id']){
			$wheres['customer_contact_log.customer_id'] = I("customer_id"); 
	    	$wheres['customer_contact_log.user_id'] = array('neq',-1);
		}else{
			$wheres['customer_contact_log.user_id'] = $this->m_user['user_id'];
			$wheres['customer_contact_log.customer_id'] = I("customer_id"); 
		}

		$res['customerLog'] = $CustomerContactLog
							  ->field("customer_contact_log.*,user.nickname")
							  ->join("user ON customer_contact_log.user_id = user.user_id")
							  ->where($wheres)
							  ->order("contact_id desc")
							  ->limit("0,3")
							  ->select();
		$Calllog = M("calllog");
		foreach ($res['customerLog'] as &$value) {
			if($value['cdr_id']==""){
				$value['calltime'] = "";
			}else{
				$call_wheres['call_id'] = $value['cdr_id'];
				$value['calltime']= $Calllog->field("calltime")->where($call_wheres)->find();
				if($value['calltime']['calltime'] > 0){
					$value['calltime'] = floor($value['calltime']['calltime']/60);
				}
			}
		}
		$count = count($res['customerLog']);
		$Page = new \Common\ORG\Page( $count, $number );
		$p = I("p");
		$psize = I("psize");
		for($i=($p-1)*$psize;$i<$p*$psize;$i++){
    	 	$new_list[$i] = $res['customerLog'][$i];
    	 }
    	 foreach ($new_list as $new_key =>$new_val) {
    		if(!empty($new_val)){
    			$res['new_log'][] = $new_val;
    		}
    	}
    	
		$res['page'] =  $Page->getTotalPages();
		$res['p'] = $Page->nowPage;
		$res['psize'] = $number;
		e_json($res);
	}
	public function ChangeCustomerType(){
		//修改客户类型
		$CustomerXufei = M("customer_xufei");
		$where['customer_xufei_id'] = I("customer_xufei_id");
		$arr = $CustomerXufei->where($where)->find();
		$map['customer_type'] = I("new_type");
	
		$Customer = M("customer");
		$wheres['customer_id'] = I("customer_id");
		$list = $Customer->where($wheres)->find();
		$maps['note'] = I("new_note");
		$maps['mount'] = I("mount");
			
		if($arr['customer_type'] == $map['customer_type'] && $list['note'] == $maps['note'] && $list['mount'] == $maps['mount']){
			$res['rcode'] = "1";
		}else{
			$type = $CustomerXufei->where($where)->save($map);
			$note = $Customer->where($wheres)->save($maps);
			if($type || $note){
				$res['rcode'] = "1";
			}else{
				$res['rcode'] = "修改失败!";
			}
		}
		e_json($res);
	}
	public function AddCommunication(){
		//添加沟通记录
		$CustomerContactLog = M("customer_contact_log");
		$map['customer_id'] = I("customer_id");
		$map['user_id'] = $this->m_user['user_id'];
		$map['user_sector'] = $this->m_user['sector_id'];
		$map['user_role'] = $this->m_user['role_id'];
		$map['create_time'] = date("Y-m-d H-i-s");
		$map['content'] = I("communication");
		$map['stock'] = I("recommended");
		$map['cdr_id'] = "";
		$map['is_delete'] = 0;
		$list = $CustomerContactLog->add($map);
		if($list){
			$res['rcode'] = "1";
		}else{
			$res['rcode'] = "添加失败!";
		}
		e_json($res);
	}
	public function Tab_Select(){
		//标签加载
		$CcustomerTagList = M("customer_tag_list");
		$where['customer_id'] = I("customer_id");
		$list = $CcustomerTagList
				->field("customer_tag.tag_name,customer_tag.tag_id")
				->join("customer_tag on customer_tag_list.tag_id = customer_tag.tag_id")
				->where($where)->select();
		$res['list'] = $list;

		foreach ($list as &$value) {
			$value = $value['tag_id'];
		}

		$v = $list;
		$res['child'] = implode(',',$v);

		$CcustomerTag = M("customer_tag");	
		$res['slist'] = $CcustomerTag->field("tag_name,tag_id")->order("tag_id desc")->select();
		$res['unslist'] = $res['slist'];
		foreach ($res['unslist'] as $keyss => $val) {
			foreach ($res['list'] as $k => $v) {
				if($v['tag_id'] == $val['tag_id'] ){
					 unset($res['unslist'][$keyss]);//删除$a数组同值元素
				}
			}
		}
		foreach ($res['unslist'] as $new_key => $new_value) {
			$res['new_list'][] = $new_value;
		}
		$res['tag_customer']['customer_id'] = I("customer_id");
		$res['tag_customer']['user_id'] = I("user_id");
		e_json($res);
	}
	public function Check_Tag(){
		//添加标签
		$CustomerTagList = M("customer_tag_list");
		$arr_tag = I("get.array_tag");
		$arr = explode(',', $arr_tag);
		$num = count($arr);
		if($num <= 3){
			$where['customer_id'] = I("customer_id");
			$del_tag = $CustomerTagList->where($where)->delete();
			foreach ($arr as $key => $value) {
				$map['user_id'] = I("user_id");
				$map['customer_id'] = I("customer_id");
				$map['tag_id'] = $value;
				$map['create_time'] = date("Y-m-d H-i-s");
				$map['count'] = 0;	
				$list = $CustomerTagList->add($map);
			}
		}
		if($list){
			$res['rcode'] = "1";
		}else{
			if($num > 3 ){
				$res['rcode'] = "标签最多不能大于3组!";
			}else{
				$res['rcode'] = "添加失败!";	
			}
		}
		e_json($res);
	}
	public function New_Tag(){
		
		//创建新标签、用户添加新标签
		$CustomerTag = M("customer_tag");
		$where['tag_name'] = I("new_tag_name");
		$where['create_time'] = date("Y-m-d H-i-s");
		$where['creator'] = I("user_id");
		$where['count'] = '0';
		$arr = $CustomerTag ->add($where);
		$res['tag_customer'] = I("customer_id");
		if($arr){
			$CustomerTagList = M("customer_tag_list");
			$maps['customer_id'] = I("customer_id");
			$count = $CustomerTagList->where($maps)->count();
	
			$wheres['tag_id'] = $arr;
			if($count < 3){
				$map['user_id'] = I("user_id");
				$map['customer_id'] = I("customer_id");
				$map['tag_id'] = $arr;
				$map['create_time'] = date("Y-m-d H-i-s");
				$map['count'] = 0;
				$list = $CustomerTagList->add($map);

				if($list){
					$res['msg'] = "1";
					$res['rcode'] = $CustomerTag ->field("tag_id,tag_name")->where($wheres)->find();
					$res['lists'] = $CustomerTag ->field("tag_id,tag_name")->select();
					$vmap['customer_tag_list.customer_id'] = I("customer_id");
					$res['customer_tag'] = $CustomerTagList
											->field("customer_tag.tag_name,customer_tag.tag_id")
											->join("customer_tag ON customer_tag_list.tag_id = customer_tag.tag_id")
											->where($vmap)
											->select();
					$res['tag_list'] = implode(array_column($res['customer_tag'], 'tag_id'),',');
				}else{																													
					$delete = $CustomerTag ->where($wheres)->delete();
					$res['rcode'] = "添加失败!";
				}
			}else{
				$delete = $CustomerTag ->where($wheres)->delete();
				$res['rcode'] = "标签最多不能大于3组!";
			}	
		}
		e_json($res);
	}
	public function Holding(){
		//推荐股票加载
		$where['customer_xufei_id'] = I("customer_xufei_id");
		$stock = M("customer_stock")->where($where)->order("customer_stock_id desc")->limit(0,5)->select();
		if($stock){
			foreach ($stock as &$value) {
				$value['buy_time'] = substr($value['buy_time'], 5,5);
				$value['profit'] = $value['profit']*100;
			}
			$res['rcode'] = "1";
			$res['list'] = $stock;
			$res['customer_xufe_id'] = I("customer_xufei_id");
		}else{
			$res['rcode'] = "2";
		}
		e_json($res);
	}
	public function StockShow(){
		//添加加载
		$arr = M("stock_list")->select();
		if($arr){
			$res['stock_list'] = $arr;
		}
		e_json($res);
	}
	 public function DeleteDefaultTag(){
		//删除默认标签
	 	$CustomerTagList = M("customer_tag_list");
		$where['customer_id'] = I("customer_id");
	 	$where['tag_id'] = I("tag_id");
	 	$del_tag = $CustomerTagList->where($where)->delete();
	 	if($del_tag){
	 		$res['rcode'] = '1';
	 	}else{
	 		$res['rcode'] = '无法取消!';
	 	}
	 	$res['tag_id'] = I("tag_id");
	 	e_json($res);
	 }
}